package kbs.scm
{
    public class PropertyFunction extends PrimitiveFunction
    {
        public function PropertyFunction(pname:String)
        {
            m_pname = pname;
        }

        override public function scmapply(alist:AList, env:Env):Base
        {
            var argc:int = alist.length();
            if (argc == 0) {
                throw new Error("property -- missing object to apply "+m_pname);
            }
            if (argc > 2) {
                throw new Error("property -- too many arguments "+m_pname+":"+argc);
            }

            var inst:Object = toObject(alist.get(0), env);
            if (argc == 1) {
                // get property
                return toBase(inst[m_pname]);
            }
            else {
                // set property
                var pval:Base = alist.get(1);
                inst[m_pname] = toObject(pval, env);
                return pval;
            }
        }

        override public function pprint():String
        {
            return "."+m_pname+"$";
        }
        private var m_pname:String;
    }
}
